package com.kb.design.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kb.design.entity.DesignProgress;
import com.kb.design.entity.DesignTalk;
import com.kb.design.mapper.postgreSQL.DesignTalkMapper;
import com.kb.design.service.DesignTalkService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;;
import java.time.LocalDateTime;
import java.util.Objects;

/**
 * 交流表 Service 实现类
 *
 * @author 通义灵码
 * @since 2023-10-10
 */
@Service
@DS("slave-1")
public class DesignTalkServiceImpl extends ServiceImpl<DesignTalkMapper, DesignTalk> implements DesignTalkService {

    @Resource
    private DesignTalkMapper designTalkMapper;
    @Override
    public Page<DesignTalk> pageList(Page<DesignTalk> page, DesignTalk designTalk) {
        LambdaQueryWrapper<DesignTalk> eq = Wrappers.lambdaQuery(DesignTalk.class).eq(DesignTalk::getProjectId, designTalk.getProjectId())
                .eq(DesignTalk::getStatus, designTalk.getStatus()).eq(DesignTalk::getDelFlag, 0);
        if (Objects.equals(designTalk.getOrderDir(), "desc")){
            eq.orderByDesc(DesignTalk::getCreateTime);
        }else {
            eq.orderByAsc(DesignTalk::getCreateTime);
        }

        return designTalkMapper.selectPage(page,eq);
    }

    @Override
    public boolean save(DesignTalk designTalk) {
        if (designTalk.getId() != null) {
            return designTalkMapper.updateById(designTalk) > 0;
        }else {
//        designTalk.setId( null);
            return  designTalkMapper.insert(designTalk) > 0;
        }
    }

    @Override
    public boolean del(DesignTalk designTalk) {
        designTalk.setDelFlag(1); // 标记为已删除
        designTalk.setUpdateTime(LocalDateTime.now());
      return designTalkMapper.updateById(designTalk) > 0;
    }

    // 可以在这里添加自定义的业务方法
}
